/*
 * Created on Jan 10, 2005
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package org.scohen.juploadr.uploadapi.zooomr.authentication;

import java.io.IOException;
import java.io.StringReader;
import java.util.SortedMap;
import java.util.TreeMap;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpMethodBase;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.PostMethod;
import org.scohen.juploadr.Messages;
import org.scohen.juploadr.uploadapi.AuthException;
import org.scohen.juploadr.uploadapi.ProtocolException;
import org.scohen.juploadr.uploadapi.zooomr.DefaultZooomrHandler;
import org.scohen.juploadr.uploadapi.zooomr.ZooomrMethod;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.XMLReaderFactory;

/**
 * @author steve
 * 
 * TODO To change the template for this generated type comment go to Window -
 * Preferences - Java - Code Style - Code Templates
 */
public class ZooomrAuth extends ZooomrMethod  {
    private String url = "http://zooomr.com/services/auth";
    /*
     * <?xml version="1.0" encoding="utf-8" ?> <user> <username>Bees </username>
     * <status> <pro>1 </pro> </status> <transfer> <limit>107374182400 </limit>
     * <used>8925364 </used> </transfer> </user>
     * 
     * <?xml version="1.0" encoding="utf-8" ?> <rsp stat="fail"> <err code="1"
     * msg="Invalid login details" /> </rsp>
     * 
     * 
     */
    private String username;
    private String password;
    private boolean isPro = false;
    private long monthlyUploadLimit;
    private long currentUploadUsed;
    private String failureReason;
    private boolean successful = true;

    public ZooomrAuth(String username, String password, HttpClient client) {
        this.username = username;
        this.password = password;
    }

    /**
     * @param responseBodyAsString
     * @return
     * @throws AuthException
     */
    public boolean parseResponse(String response) throws ProtocolException {
        try {
            System
                    .setProperty("org.xml.sax.driver",
                            "com.bluecast.xml.Piccolo");
            XMLReader reader = XMLReaderFactory.createXMLReader();
            ZooomrAuthHandler handler = new ZooomrAuthHandler(this);
            reader.setContentHandler(handler);
            reader.parse(new InputSource(new StringReader(response)));
            if (!isSuccessful()) {
                throw new AuthException(Messages
                        .getString("juploadr.ui.error.auth.failed")
                        + getFailureReason());
            }
            return isSuccessful();
        } catch (SAXException e) {
            throw new AuthException(
                    Messages
                            .getString("juploadr.ui.error.response.unreadable.noreason"),
                    e);
        } catch (IOException e) {
            // this can't happen
        }
        return false;
    }

    /**
     * @return Returns the isPro.
     */
    public boolean isPro() {
        return isPro;
    }

    /**
     * @param isPro
     *            The isPro to set.
     */
    public void setPro(boolean isPro) {
        this.isPro = isPro;
    }

    /**
     * @return Returns the password.
     */
    public String getPassword() {
        return password;
    }

    /**
     * @param password
     *            The password to set.
     */
    public void setPassword(String password) {
        this.password = password;
    }

    /**
     * @return Returns the username.
     */
    public String getUsername() {
        return username;
    }

    /**
     * @param username
     *            The username to set.
     */
    public void setUsername(String username) {
        this.username = username;
    }

    /**
     * @return Returns the currentUploadUsed.
     */
    public long getCurrentUploadUsed() {
        return currentUploadUsed;
    }

    /**
     * @param currentUploadUsed
     *            The currentUploadUsed to set.
     */
    public void setCurrentUploadUsed(long currentUploadUsed) {
        this.currentUploadUsed = currentUploadUsed;
    }

    /**
     * @return Returns the monthlyUploadLimit.
     */
    public long getMonthlyUploadLimit() {
        return monthlyUploadLimit;
    }

    /**
     * @param monthlyUploadLimit
     *            The monthlyUploadLimit to set.
     */
    public void setMonthlyUploadLimit(long monthlyUploadLimit) {
        this.monthlyUploadLimit = monthlyUploadLimit;
    }

    /**
     * @return Returns the failureReason.
     */
    public String getFailureReason() {
        return failureReason;
    }

    /**
     * @param failureReason
     *            The failureReason to set.
     */
    public void setFailureReason(String failureReason) {
        this.failureReason = failureReason;
        successful = false;
    }

    /**
     * @return Returns the succeeded.
     */
    public boolean isSuccessful() {
        return successful;
    }

    /**
     * @param succeeded
     *            The succeeded to set.
     */
    public void setSuccessful(boolean succeeded) {
        this.successful = succeeded;
    }

    public DefaultZooomrHandler getResponseHandler() {
        return new ZooomrAuthHandler(this);
    }

    public HttpMethodBase getMethod() {
        PostMethod post = new PostMethod(url);
        post.addParameter(new NameValuePair("email", username));
        post.addParameter(new NameValuePair("password", password));
        return post;
    }

    public SortedMap<String, String> getParams() {
        SortedMap<String, String> params = new TreeMap<String, String>();
        params.put("email", username);
        params.put("password", password);
        return params;
    }

    public boolean isAuthorized() {
        return false;
    }

}
